import com.hope.entity.Student;
import com.hope.service.UserService;
import com.hope.utils.DruidUtil;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;

public class test {
    //        1.创建spring的核心容器
    private ApplicationContext apl = new ClassPathXmlApplicationContext("spring.xml");

    @Test
    public void test1(){
//        2.通过getBean获取要使用的对象
//        apl.getBean("userService");获取到的是实现类的对象
//        多态——>接口指向实现类 UserService userService指向的是接口
        UserService userService = (UserService) apl.getBean("userService");
        System.out.println(userService.getName());
    }

    /**
     * 单例和多例
     */
    @Test
    public void studentTest(){
        Student student1 = (Student) apl.getBean("student");
        Student student2 = (Student) apl.getBean("student");
        System.out.println(student1);
        System.out.println(student2);
    }

    /**
     * 懒加载
     */
    @Test
    public void test3(){
        Student student3 = (Student) apl.getBean("student");
        ((ConfigurableApplicationContext)apl).close();
    }

    /**
     * 无参构造实例化bean-getbean去使用对象
     */
    @Test
    public void test4(){
        Student student3 = (Student) apl.getBean("student");
    }


    /**
     * 静态方法方法实例化dataSource
     */
    @Test
    public void test5(){
//        获取工厂创建的dataSource对象
        DataSource dataSource= (DataSource) apl.getBean("druid");
        System.out.println(dataSource);
    }

}
